<?php
namespace app\api\controller;
use app\service\cxyunPayService;
use app\service\cxyunService;
use app\service\roominfoService;
use think\Controller;
use think\Exception;
use think\Request;
use think\Db;

class Test extends Controller{
    public function test5(){
        echo "test6";
    }
    public function test6(){
        $info=Db::table("cxyun_pay_bill")->where("id",8)->find();
        $payNotice["parkingId"]="5292718323470392139";
        $payNotice["plateNumber"]="浙A5FZ99";
        $payNotice["payFee"]=5;
        $payNotice["orderNo"]="5622845453149211814";
        $payNotice["payType"]=5;
        $payNotice["timestamp"]=time()."000";
        $payNotice["orderId"]=$info["recordId"];//$info["id"];
        $cxyunApi=new cxyunService();
        $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
        var_dump($res);
        //var_dump($info);
    }
    public function test4(){
        $info=Db::table("cxyun_pay_bill")->where("id",8)->find();
        $payNotice["parkingId"]=$info["parkingId"];
        $payNotice["plateNumber"]=$info["plateNumber"];
        $payNotice["payFee"]=$info["amount"];
        $payNotice["orderNo"]="5622845453149211814";
        $payNotice["payType"]=5;
        $payNotice["timestamp"]=time()."000";
        $payNotice["orderId"]=$info["recordId"];//$info["id"];
        $cxyunApi=new cxyunService();
        $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
        var_dump($res);
        //var_dump($info);
    }
    public function test(){
        $path=dir(getcwd())->path.'/pay/20240111/cxyunpay';
        $address = $path."/200329642124.txt";
        $con=file_get_contents($address);
        $data=json_decode($con,true);
        
        $info["parkingId"]=$data["parkId"];
        $info["plateNumber"]=$data["carNo"];
        $info["recordId"]=$data["onlyCode"];
        $info["amount"]=$data["amount"];
        $info["cartypeName"]=$data["carType"];
        $info["carmodelName"]=$data["carSize"];
        $info["inLaneCode"]=$data["inLaneCode"];
        $info["outLaneCode"]=$data["outLaneCode"];

        $payNotice["parkingId"]=$info["parkingId"];
        $payNotice["plateNumber"]=$info["plateNumber"];
        $payNotice["payFee"]=$info["amount"];
        $payNotice["orderNo"]=$info["recordId"];
        $payNotice["payType"]=262;
        $payNotice["timestamp"]=time()."000";
        $cxyunApi=new cxyunService();
        //改订单是否支付
        $payInfo=(new cxyunPayService())->getPayInfo($info);

        if(empty($payInfo)){
            //查询是否是月租车或福利车
            $carInfo=(new cxyunPayService())->getCarInfo($info);
            
            $info["car_id"]=$carInfo["id"];
            $info["carType"]=$carInfo["carType"];
            if(!empty($carInfo)){
                //查询房屋金额
                $roominfo=(new roominfoService())->getRoomInfo($carInfo["room_id"]);
                if(empty($roominfo)){
                    return json(["status"=>0,"message"=>"房屋号不存在,请重新绑定车牌"]);
                }
                if($carInfo["carType"]==1){
                    if($roominfo["monthly_car_balance"]<$info["amount"]){
                        return json(["status"=>0,"message"=>"余额不足"]);
                    }
                    $result=(new cxyunPayService())->monthlyCarPay($info,$roominfo);
                    if($result["code"]==0){
                        $payNotice["orderId"]=$result["billid"];

                        $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                        var_dump($res);
                        return json(["status"=>1,"message"=>"支付成功"]);
                    }else{
                        return json(["status"=>500,"message"=>$result["msg"]]);
                    }
                }else if($carInfo["carType"]==2){
                    if($roominfo["welfare_car_balance"]<$info["amount"]){
                        return json(["status"=>0,"message"=>"余额不足"]);
                    }
                    //福利车 扣除房屋福利车金额
                    $result=(new cxyunPayService())->welfareCarPay($info,$roominfo);
                    if($result["code"]==0){
                        $payNotice["orderId"]=$result["billid"];

                        $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                        var_dump($res);
                        return json(["status"=>1,"message"=>"支付成功"]);
                    }else{
                        return json(["status"=>500,"message"=>$result["msg"]]);
                    }
                }
            }

        }else{
            $payNotice["orderId"]=$payInfo["id"];
            $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
            //var_dump($res);
            return json(["status"=>1,"message"=>"成功"]);;
        }
        //$info[""]
        var_dump($data);
    }
    public function recordadd(){
        $path=dir(getcwd())->path.'/pay/'.date('Ymd')."/cxyunrecord";
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
       // echo "进出记录推送";
        return json(["status"=>1,"message"=>""]);
    }
    
    public function pay(){
        //file_get_contents("php://input");
        $path=dir(getcwd())->path.'/pay/'.date('Ymd')."/cxyunpay";
        //$path=dir(getcwd())->path.'/pay/cxyunpay/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        echo "进出记录推送";
    }
    public function payrecord(){
        //file_get_contents("php://input");
        $path=dir(getcwd())->path.'/pay/'.date('Ymd')."/cxyunpayrecord";
        //$path=dir(getcwd())->path.'/pay/cxyunpay/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        
        return json(["status"=>1,"message"=>""]);
    }
    
     public function test2(){
        $path=dir(getcwd())->path.'/pay/20240111/cxyunrecord';//cxyunpay/
        $address = $path."/200209219708.txt";
        $con=file_get_contents($address);
        $data=json_decode($con,true);
        $info["parkingId"]=$data["park_id"];
        $info["parkingNume"]=$data["park_name"];
        $info["recordId"]=$data["record_id"];
        $info["plateNumber"]=$data["plate_number"];
        $info["plateColor"]=$data["plate_color"];
        //查询是否四月租车或福利车
        $carInfo=Db::table("cxyun_car")
                ->where("parkingId",$info["parkingId"])
                ->where("plateNumber",$info["plateNumber"])
                ->find();
                
        $info["car_id"]=$carInfo["id"];
        $info["carType"]=$carInfo["carType"];
        if(empty($carInfo)){
            return json(["status"=>1,"message"=>""]);
        }
        //
        $recordInfo=Db::table("cxyun_entry_exit_record")
                ->where("recordId",$info["recordId"])
                ->find();
        
        //入场
        if($data["is_exit"]=="0"){
           $info["enterReleaseType"]=$data["release_type"];
           $info["enterRemark"]=$data["remark"];
           $info["entranceTime"]=$data["entrance_time"];
           $info["entranceGateId"]=$data["entrancegate_id"];
           $info["entranceGateName"]=$data["entrancegate_name"];
        }else{
            //出场
           $info["exitReleaseType"]=$data["release_type"];
           $info["exitRemark"]=$data["remark"];
           $info["exitGateName"]=$data["exitgate_name"];
           $info["exitGateId"]=$data["exitgate_id"];
           $info["exitTime"]=$data["exit_time"];
        }
        if(empty($recordInfo)){
           $res= Db::table("cxyun_entry_exit_record")->insert($info);
           if($res){
                return json(["status"=>1,"message"=>"入场成功"]);
           }else{
                return json(["status"=>0,"message"=>"入场失败"]); 
           }
        }else{
            $res= Db::table("cxyun_entry_exit_record")->where("id",$recordInfo["id"])->update($info);
            if($res){
                return json(["status"=>1,"message"=>"入场2成功"]);
           }else{
                return json(["status"=>0,"message"=>"入场2失败"]); 
           }
        }
        //cxyun_entry_exit_record
        echo "<pre>";
        var_dump($data);
         
     }
     public function test3(){
        $path=dir(getcwd())->path.'/pay/20240111/cxyunrecord';//cxyunpay/
        $address = $path."/200538112011.txt";
        $con=file_get_contents($address);
        $data=json_decode($con,true);
        $info["parkingId"]=$data["park_id"];
        $info["parkingNume"]=$data["park_name"];
        $info["recordId"]=$data["record_id"];
        $info["plateNumber"]=$data["plate_number"];
        $info["plateColor"]=$data["plate_color"];
        //查询是否四月租车或福利车
        $carInfo=Db::table("cxyun_car")
                ->where("parkingId",$info["parkingId"])
                ->where("plateNumber",$info["plateNumber"])
                ->find();
                
        $info["car_id"]=$carInfo["id"];
        $info["carType"]=$carInfo["carType"];
        if(empty($carInfo)){
            return json(["status"=>1,"message"=>""]);
        }
        //
        $recordInfo=Db::table("cxyun_entry_exit_record")
                ->where("recordId",$info["recordId"])
                ->find();
        
        //入场
        if($data["is_exit"]=="0"){
           $info["enterReleaseType"]=$data["release_type"];
           $info["enterRemark"]=$data["remark"];
           $info["entranceTime"]=$data["entrance_time"];
           $info["entranceGateId"]=$data["entrancegate_id"];
           $info["entranceGateName"]=$data["entrancegate_name"];
        }else{
            //出场
           $info["exitReleaseType"]=$data["release_type"];
           $info["exitRemark"]=$data["remark"];
           $info["exitGateName"]=$data["exitgate_name"];
           $info["exitGateId"]=$data["exitgate_id"];
           $info["exitTime"]=$data["exit_time"];
        }
        if(empty($recordInfo)){
           $res= Db::table("cxyun_entry_exit_record")->insert($info);
           if($res){
                return json(["status"=>1,"message"=>"入场成功"]);
           }else{
                return json(["status"=>0,"message"=>"入场失败"]); 
           }
        }else{
            $res= Db::table("cxyun_entry_exit_record")->where("id",$recordInfo["id"])->update($info);
            if($res){
                return json(["status"=>1,"message"=>"入场2成功"]);
           }else{
                return json(["status"=>0,"message"=>"入场2失败"]); 
           }
        }
        
        //cxyun_entry_exit_record
        echo "<pre>";
        var_dump($data);
         
     }
    
}